package joomz;

import java.nio.charset.Charset;

public final class Options
    extends java.util.HashMap<String,String>
{
    public final static Charset UTF8 = Charset.forName("UTF-8");

    public static class Option {

        public final static java.util.List<Option> Set = new java.util.ArrayList<Option>();


        public final String[] names;

        public final String description;

        public final String synopsis;

        public Option(String[] names, String description){
            super();
            this.names = names;
            this.description = description;
            Set.add(this);
            StringBuilder strbuf = new StringBuilder();
            for(String name: names){
                if (0 < strbuf.length())
                    strbuf.append('|');
                strbuf.append(name);
            }
            this.synopsis = strbuf.toString();
        }
    }


    public Options(String[] argv){
        super();
        for (int cc = 0, argc = argv.length; cc < argc; ){
            String name = argv[cc++];
            if (cc < argc){
                String value = argv[cc];
                if ('-' != value.charAt(0)){
                    cc += 1;
                    this.put(name,value);
                }
                else
                    this.put(name,"");
            }
            else
                this.put(name,"");
        }
    }


    public boolean includes(Option keys){
        return (null != this.get(keys));
    }
    public String get(Option opt){
        for (String key : opt.names){
            String value = this.get(key);
            if (null != value)
                return value;
        }
        return null;
    }
    public java.io.File getFile(Option opt){
        String value = this.get(opt);
        if (null != value)
            return new java.io.File(value);
        else
            return null;
    }
    public java.net.URL getUrl(Option opt){
        String value = this.get(opt);
        if (null != value)
            try {
                return new java.net.URL(value);
            }
            catch (java.net.MalformedURLException exc){
                try {
                    return new java.net.URL("file:"+value);
                }
                catch (java.net.MalformedURLException exc2){
                    throw new IllegalStateException(value,exc2);
                }
            }
        else
            return null;
    }
    public java.net.URL getUrl(Option opt, java.net.URL def){
        java.net.URL url = this.getUrl(opt);
        if (null != url)
            return url;
        else
            return def;
    }
    public java.io.InputStream openInput(Option opt, boolean std)
        throws java.io.IOException
    {
        java.net.URL url = this.getUrl(opt);
        if (null != url)
            return url.openStream();
        else if (std)
            return System.in;
        else
            return null;
    }
    public java.io.OutputStream openOutput(Option opt, boolean std)
        throws java.io.IOException
    {
        java.net.URL url = this.getUrl(opt);
        if (null != url){
            java.net.URLConnection urlc = url.openConnection();
            urlc.setDoOutput(true);
            return urlc.getOutputStream();
        }
        else if (std)
            return System.out;
        else
            return null;
    }
    public java.io.Reader openReader(Option opt, boolean std)
        throws java.io.IOException
    {
        java.io.InputStream stream = this.openInput(opt,std);
        if (null != stream)
            return new java.io.InputStreamReader(stream,UTF8);
        else
            return null;
    }
    public java.io.Writer openWriter(Option opt, boolean std)
        throws java.io.IOException
    {
        java.io.OutputStream stream = this.openOutput(opt,std);
        if (null != stream)
            return new java.io.OutputStreamWriter(stream,UTF8);
        else
            return null;
    }
    public java.io.BufferedReader openBufferedReader(Option opt, boolean std)
        throws java.io.IOException
    {
        java.io.Reader reader = this.openReader(opt,std);
        if (null != reader)
            return new java.io.BufferedReader(reader);
        else
            return null;
    }
}